package net.googlecode.kharchenko.controllers;

import net.googlecode.kharchenko.services.SecurityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;


import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;

@Controller
/**
 *  Controller of login page.
 */
public class IndexController {

    /**
     * Our Security service.
     */
    @Autowired
    SecurityService ss;


    protected static final Logger logger = Logger.getLogger(IndexController.class);


    /**
     * Shows login page.
     *
     * @param response server's answer
     * @return login page if user is don't autentificated, else returns home page.
     * @throws IOException
     */
    @RequestMapping(value = {"/index", "/"})
    public ModelAndView handleRequest(HttpServletResponse response)
            throws IOException {

        logger.debug("Check if the user is guest");
        if (!ss.getCurrentUser().equals("guest")) {
            logger.info("Redirecting to home");
            response.sendRedirect("home");
        }

        logger.info("Returning login view");
        return new ModelAndView("index");
    }

}